package com.ruoyi.business.controller;

import com.ruoyi.business.domain.BusCustomer;
import com.ruoyi.business.service.IBusCustomerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@Api(tags = "客户管理接口")
@RequestMapping("business/customer")
public class CustomerController extends BaseController {

    @Autowired
    private IBusCustomerService customerService;

    /**
     * 分页查询客户的列表
     * 获取当前业务员
     */
    @GetMapping("list")
    @ApiOperation("分页查询客户列表")
    public TableDataInfo getCustomerPage(BusCustomer busCustomer) {
        // 分页
        startPage();
        LoginUser loginUser = getLoginUser();
        List<BusCustomer> busCustomers = customerService.selectCustomerPage(loginUser, busCustomer);
        return getDataTable(busCustomers);
    }
    /**
     * 后端的对象怎么变成json返回给前端的
     * HttpMessageConvert  默认 jackson 转换的
     * 前端都是字符串
     * 业务代码都在service里面写  controller 接收请求和 返回数据
     *
     * @param busCustomer
     * @return
     */
    @PostMapping
    @ApiOperation("新增客户")
    public AjaxResult addCustomer(@RequestBody BusCustomer busCustomer){
        //新增就是往数据里面插入记录，给他填充默认值
        LoginUser loginUser= getLoginUser();
        return toAjax(customerService.addCustomer(busCustomer,loginUser));
    }
    @GetMapping("{id}")
    @ApiOperation("根据客户的id回显客户的信息")
    public AjaxResult getCustomerInfo(@PathVariable("id") Long id){
        BusCustomer busCustomer = customerService.selectBusCustomerById(id);
        return AjaxResult.success(busCustomer);
    }
    @PutMapping
    @ApiOperation("修改客户")
    public AjaxResult updateCustomer(@RequestBody BusCustomer busCustomer){
        //获取当前用户 是谁操作的这一条记录
        LoginUser loginUser = getLoginUser();
        return toAjax(customerService.updateCustomer(busCustomer,loginUser));
    }
    @DeleteMapping("{ids}")
    @ApiOperation("删除用户")
    public AjaxResult delCustomer(@PathVariable("ids")List<Long> ids){
        //假删除 修改del_flag=1
        LoginUser loginUser = getLoginUser();
        return toAjax(customerService.delCustomer(ids,loginUser));
    }


}
